package la.bbs558.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import la.bbs558.aop.valid.Check;
import la.bbs558.aop.valid.Valid;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.BbsBoard;
import la.bbs558.entity.params.SectionSearchPage;
import la.bbs558.entity.params.SectionUpdateParams;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.BbsBoardService;
import la.bbs558.service.BbsSectionService;
import la.bbs558.util.Objs;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Shanks
 * @date: 2023/12/15 22:41
 * @desc:
 */
@RestController
@RequestMapping("/admin/section")
@RequiredArgsConstructor
@SaCheckRole(value = { "admin" },type = StpAdminUtil.TYPE)
@Check
public class BbsSectionManageController {

    private final BbsSectionService bbsSectionService;

    @Resource
    private BbsBoardService bbsBoardService;

    @GetMapping("list")
    public Result<?> list(SectionSearchPage searchPage) {
        bbsSectionService.listSectionByPageForAdmin(searchPage);
        return Result.success(searchPage);
    }

    @GetMapping("allBoardList")
    public Result<?> allBoardList() {
        List<BbsBoard> list = bbsBoardService.lambdaQuery()
                .select(BbsBoard::getId, BbsBoard::getBoardName)
                .list();
        return Result.success(list);
    }

    @GetMapping("boardList")
    public Result<?> boardListBySectionId(Long sectionId) {
        Objs.isTrue(sectionId != null, "分区ID不能为空");
        List<BbsBoard> list = bbsBoardService.lambdaQuery()
                .eq(BbsBoard::getSectionId, sectionId)
                .list();
        return Result.success(list);
    }

    @PostMapping("add")
    public Result<?> add(@RequestBody @Valid SectionUpdateParams updateParams) {
        bbsSectionService.addSectionForAdmin(updateParams);
        return Result.success();
    }

    @PostMapping("update")
    public Result<?> update(@RequestBody @Valid SectionUpdateParams updateParams) {
        bbsSectionService.updateSectionForAdmin(updateParams);
        return Result.success();
    }

    @PostMapping("delete")
    public Result<?> delete(Long id) {
        Objs.isTrue(id != null, "分区ID不能为空");
        bbsSectionService.removeSectionByIdForAdmin(id);
        return Result.success();
    }
}
